package com.lz.javaassis;

import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.AttributeInfo;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.DeprecatedAttribute;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.SourceFileAttribute;
import javassist.bytecode.annotation.Annotation;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * TEst..
 *
 * @author Lizhong
 * @date 2021/8/29
 */
public class TEst {
    public static void main(String[] args) {
        // for example
        String classFilePath = "E:\\gihubSource\\verifyCode\\target\\classes\\com\\jh\\jcs\\common\\license\\JinherLicense.class";
        try (DataInputStream dis = new DataInputStream(new FileInputStream(new File(classFilePath)))) {
            ClassFile classFile = new ClassFile(dis);
            int minorVersion = classFile.getMajorVersion();
            System.out.println("获取当前的class文件的jdk编译版本信息:" + minorVersion);
            String[] interfaces = classFile.getInterfaces();
            System.out.println("当前classFile中的所有的接口名称为：" + Arrays.toString(interfaces));
            System.out.println("开始输出常量池中的内容：");
            ConstPool constPool = classFile.getConstPool();
            Set<String> classNames = constPool.getClassNames();
            System.out.println("classNames=" + classNames);
            boolean isInterface = classFile.isInterface();
            System.out.println("当前的这个类是否为接口？" + isInterface);
            // 获取这个类上面的注解属性
            List<AttributeInfo> attributes = classFile.getAttributes();
            if (attributes == null || attributes.isEmpty()) {
                System.out.println("没有任何的属性！");
            }
            for (AttributeInfo attributeInfo : attributes) {
                // 获取注解属性
                if (attributeInfo instanceof AnnotationsAttribute) {
                    AnnotationsAttribute attr = (AnnotationsAttribute) attributeInfo;
                    Annotation[] annotations = attr.getAnnotations();
                    System.out.println("解析获得注解：" + Arrays.toString(annotations));
                } else if (attributeInfo instanceof DeprecatedAttribute) {
                    // 获取DeprecatedAttribute属性
                    DeprecatedAttribute attr = (DeprecatedAttribute) attributeInfo;
                    System.out.println("attr=" + attr.tag);
                } else if (attributeInfo instanceof SourceFileAttribute) {
                    // 获取源文件属性
                    SourceFileAttribute attr = (SourceFileAttribute) attributeInfo;
                    String fileName = attr.getFileName();
                    System.out.println("fileName=" + fileName);
                }
            }
            // 由于获取了注解所以可以使用各种其他的操作

            // 获取当前的方法
            System.out.println("输出当前的所有的方法：");
            List<MethodInfo> methods = classFile.getMethods();
            // if (methods == null || methods.isEmpty()) {
            //     System.out.println("没有任何方法.....");
            // } else {
            //     for (MethodInfo methodInfo : methods) {
            //         System.out.println(methodInfo.getName() + "," + methodInfo.getDescriptor());
            //     }
            // }
            MethodInfo vl = classFile.getMethod("vl");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
